<template>
  <div>
    <button @click="suoding">{{ state }}</button>
    <select v-model="xia">
      <option value="">请选择城市</option>
      <option v-for="item in xiala" :key="item.id">
        {{ item.name }}
      </option>
    </select>
    <div>用户名:<input v-model="user" type="text" :disabled="suo" /></div>
    <div>年龄:<input type="number" v-model="age" :disabled="suo" /></div>
    <button @click="add" :disabled="suo">提交</button>
    <table class="tab" border="1">
      <tr>
        <th>删除所选</th>
        <th>id</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>城市</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item, index) in list" :key="index">
        <td><input type="checkbox" v-model="item.ss" /></td>
        <td>{{ item.id }}</td>
        <td>{{ item.user }}</td>
        <td>{{ item.age }}</td>
        <td>{{ item.xia }}</td>
        <td>
          <button :disabled="suo" @click="xiu(item)">修改</button
          ><button :disabled="suo" @click="del(index)">删除</button>
        </td>
      </tr>
    </table>
    <button :disabled="suo" @click="removes">删除所选</button>
    <!--修改的弹出框 -->
    <div class="dilog" v-show="isshow">
      <div class="div">
        <div>
          用户名:<input v-model="updatauser" type="text" :disabled="suo" />
        </div>
        <div>
          年龄:<input type="number" v-model="updataage" :disabled="suo" />
        </div>
        <select v-model="updataxia">
          <option v-for="item in xiala" :key="item.id">
            {{ item.name }}
          </option>
        </select>
        <button @click="qu">取消</button><button @click="ok">确定</button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      // 修改的名字
      updatauser: "",
      // 修改的年龄
      updataage: "",
      // 修改的城市
      updataxia: "",
      // 下拉菜单的值
      xia: "",
      // 姓名
      user: "",
      // 年龄
      age: "",
      // 保存下拉菜单的数组
      xiala: [],
      // 展示学生信息的数据
      list: [],
      // 提交的时候为每一条数据添加id
      id: 0,
      // 控制按钮禁用启用的变量
      suo: false,
      // 锁定按钮和解锁按钮切换内容的变量
      state: "锁定",
      // 倒计时开始的秒
      num: 4,
      //清除定时器
      si: null,
      // 删除所选的状态
      ss: false,
      // 控制修改弹框的变量
      isshow: false,
    };
  },
  mounted() {
    // 请求下拉菜单的数据
    this.$axios.get("/data.json").then((res) => {
      this.xiala = res.data.data;
    });
  },
  methods: {
    // 提交
    add() {
      // 非空验证
      if (this.user == "" || this.age == "" || this.xia == "") {
        alert("姓名年龄城市为空不能添加！！！");
        return;
      } else {
        // 为每条数据添加id 点击的时候自增
        this.id++;
        // 生成一个对象把需要添加的属性保存到obj
        const obj = {
          xia: this.xia,
          user: this.user,
          age: this.age,
          id: this.id,
          ss: this.ss,
        };
        this.list.push(obj);
        // 添加完数据然后清空输入框
        this.xia = "";
        this.user = "";
        this.age = "";
      }
    },
    // 单条删除
    del(index) {
      this.list.splice(index, 1);
    },
    // 删除所选
    removes() {
      var res = this.list.filter((item) => {
        return item.ss == true;
      });
      res.forEach((val) => {
        this.list.forEach((ele, i) => {
          if (ele.id == val.id) {
            this.list.splice(i, 1);
          }
        });
      });
    },
    // 修改
    xiu(item) {
      this.isshow = true;
      this.id = item.id;
      this.updatauser = item.user;
      this.updataage = item.age;
      this.updataxia = item.xia;
    },
    // 确定修改
    ok() {
      this.isshow = false;
      this.list.forEach((ia) => {
        if (ia.id == this.id) {
          ia.user = this.updatauser;
          ia.age = this.updataage;
          ia.xia = this.updataxia;
        }
      });
    },
    // 取消修改
    qu() {
      this.isshow = false;
    },
    // 锁定解锁
    suoding() {
      // false  代表锁定
      if (this.suo == false) {
        this.suo = true;
        this.state = "解锁";
      }
      // true 代表解锁
      else if (this.suo == true) {
        if (this.si == null) {
          // 解锁倒计时效果
          this.si = setInterval(() => {
            if (this.num > 1) {
              this.num--;
              this.state = `${this.num}` + "秒后解锁";
            }
          }, 1000);
        }
        // 清除定时器 把状态变成锁定
        setTimeout(() => {
          this.suo = false;
          clearInterval(this.si);
          this.si = null;
          this.state = "锁定";
          this.num = 4;
        }, 5000);
      }
    },
  },
  computed: {},
  watch: {},
};
</script>
<style scoped>
.tab {
  width: 1000px;
  margin: 0 auto;
}
.dilog {
  width: 100%;
  height: 100%;
  background: rgba(8, 1, 1, 0.204);
  position: fixed;
  left: 0px;
  top: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.div {
  width: 300px;
  height: 300px;
  background: lightyellow;
  line-height: 60px;
}
</style>